package com.action.ui.activity.user;

import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.action.childbase.ChildBaseActivity;
import com.action.entity.params.WithDepositParam;
import com.action.utils.SingleClick;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.base.app_core.base.ActionPacketView;
import com.base.app_core.model.Paginated;
import com.base.app_core.model.UserSmsVo;
import com.base.app_core.service.pressenter.XYBUPressenter;
import com.base.app_core.service.user.model.BindAliUserAcount;
import com.base.app_core.util.AppConstants;
import com.base.app_core.util.Preferences;
import com.blankj.utilcode.util.StringUtils;
import com.xinglai.yun.R;
import com.hwangjr.rxbus.RxBus;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.OnClick;

@Route(path = AppConstants.ROUTEURI.BINDALIACCOUNT)
public class InputZhiFuBaoAccount extends ChildBaseActivity<XYBUPressenter> implements ActionPacketView.SmsAction {

    @BindView(R.id.ali_account)
    EditText aliAccount;

    @BindView(R.id.user_realname)
    EditText userRealname;

    @Inject
    WithDepositParam withDepositParam;

    @BindView(R.id.login_request_code)
    TextView mlogin_request_code;

    @BindView(R.id.hint)
    TextView hint;

    @BindView(R.id.register_tell)
    EditText smsCode;

    private String strAliAccount;
    private String strUserName;
    private int count;

    @Override
    protected void setupComponent() {
        super.setupComponent();
        getChildViewComponent().inject(this);
    }

    /**
     * 绑定支付宝获取验证码
     *
     * @param view
     */
    @OnClick(R.id.login_request_code)
    public void onClickGetSmsCodeForBindAliAccount(View view) {
        if (SingleClick.isSingle() && null != mPresenter) {
            String aliAccount = this.aliAccount.getText().toString();
            if (StringUtils.isEmpty(aliAccount)) {
                showToast("支付宝账号不允许为空");
                return;
            }

            String realName = userRealname.getText().toString();
            if (StringUtils.isEmpty(realName)) {
                showToast("名称不允许为空");
                return;
            }

            long aLong = Preferences.getDefault().getLong(AppConstants.SP.ENTER_WRONG_TIME, 0);
            if (aLong != 0 && (System.currentTimeMillis() - aLong) < AppConstants.INVALID_SMS_TIME_OUT) {
                showToast("短信验证码输入错误三次,请2个小时后再来");
                return;
            }

            mPresenter.getSmsCodeForbindAliAccount();
        } else {
            showToast("操作频繁");
        }
    }

    @OnClick(R.id.submit)
    public void bindAliAccount(View view) {
        if (SingleClick.isSingle() && null != mPresenter) {
            strAliAccount = aliAccount.getText().toString();
            strUserName = userRealname.getText().toString();
            if (StringUtils.isEmpty(aliAccount.getText().toString())) {
                showToast("支付宝账号不允许为空");
                return;
            }
            if (StringUtils.isEmpty(userRealname.getText().toString())) {
                showToast("名称不允许为空");
                return;
            }
            if (StringUtils.isEmpty(smsCode.getText().toString())) {
                showToast("验证码不能为空");
                return;
            }

            long aLong = Preferences.getDefault().getLong(AppConstants.SP.ENTER_WRONG_TIME, 0);
            if (aLong != 0 && (System.currentTimeMillis() - aLong) < 2 * 60 * 60 * 1000) {
                showToast("短信验证码输入错误三次,请2个小时后再来");
                return;
            }

            showProgress();
            mPresenter.bindAliAccount(strAliAccount, strUserName, smsCode.getText().toString());
        }
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_input_zhi_fu_bao_account;
    }

    @Override
    protected void initView() {
        super.initView();
        setToolBar("绑定支付宝");
        String aliAccount = withDepositParam.getAliAccount();
        if (!StringUtils.isEmpty(aliAccount)) {
            this.aliAccount.setText(aliAccount);
            this.aliAccount.setSelection(aliAccount.length());
        }

        String realName = withDepositParam.getRealName();
        if (!StringUtils.isEmpty(realName)) {
            userRealname.setText(realName);
            userRealname.setSelection(realName.length());
        }

        String aliAcccount = Preferences.getDefault().getString(AppConstants.SP.ALI_ACCOUNT, null);
        if (!StringUtils.isEmpty(aliAcccount)) {
            hint.setText("绑定您的支付宝帐号，提现方式为支付宝时，将默认打款至该帐号(当前已绑定:" + aliAcccount + ")");
        } else {
            hint.setText("绑定您的支付宝帐号，提现方式为支付宝时，将默认打款至该帐号!");
        }
    }

    @OnClick(R.id.submit)
    public void onViewClicked() {
        if (SingleClick.isSingle()) {
            String aliAccount = this.aliAccount.getText().toString();
            if (StringUtils.isEmpty(aliAccount)) {
                showToast("支付宝账号不允许为空");
                return;
            }
            String realName = userRealname.getText().toString();
            if (StringUtils.isEmpty(realName)) {
                showToast("名称不允许为空");
                return;
            }
            withDepositParam.setAliAccount(aliAccount);
            withDepositParam.setRealName(realName);
            Preferences.putString(AppConstants.SP.ALI_ACCOUNT, aliAccount);
            Preferences.putString(AppConstants.SP.USER_REALNAME, realName);
            RxBus.get().post(withDepositParam);
            this.finish();
        }
    }

    @Override
    public void updateSmsCode(int ntick) {
        if (ntick != 0) {
            mlogin_request_code.setText(ntick + "s");
            mlogin_request_code.setTextColor(getResources().getColor(R.color.text_grey));
        } else {
            mlogin_request_code.setText("获取验证码");
            mlogin_request_code.setClickable(true);
        }
    }

    @Override
    public void bindMobileSuccess(BindAliUserAcount data) {
        dismissProgress();
        String snCode = data.getSnCode();
        if (!StringUtils.isEmpty(snCode)) {
            Preferences.putString(AppConstants.SP.SNCODE, snCode);
            String aliAccount = data.getAliAccount();
            if (!StringUtils.isEmpty(aliAccount)) {
                withDepositParam.setAliAccount(aliAccount);
            }

            String userRealName = data.getUserRealName();
            if (!StringUtils.isEmpty(userRealName)) {
                withDepositParam.setRealName(userRealName);
            }
            showToast("绑定成功");
            finish();
        } else {
            showToast("操作失败");
        }
    }

    @Override
    public <T> void notifyData(T object, Class tClass, Paginated paginated) {
        super.notifyData(object, tClass, paginated);
        if (object instanceof UserSmsVo) {
            showToast("发送成功");
            mPresenter.countSmsSend();
        }
    }

    @Override
    public void notifyError(String errorCode, String content) {
        super.notifyError(errorCode, content);
        if ("1013".equals(errorCode)) {
            count++;
            if (count == 3) {
                Preferences.putLong(AppConstants.SP.ENTER_WRONG_TIME, System.currentTimeMillis());
            }
        }
    }
}
